package com.abe.libquick.widget.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.abe.libquick.R;
import com.abe.libquick.utils.NormalUtils;

public class FormKeyValueView extends RelativeLayout {
    private TextView itemLabelName, itemValue;
    private String label, value;

    public FormKeyValueView(Context context) {
        this(context, null);
    }

    public FormKeyValueView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FormKeyValueView(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        //抽取需要的自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.FormKeyValueView);
        int labelRes = typedArray.getResourceId(R.styleable.FormKeyValueView_labelRes, 0);
        String labelStr = typedArray.getString(R.styleable.FormKeyValueView_label);
        int valueRes = typedArray.getResourceId(R.styleable.FormKeyValueView_valueRes, 0);
        String valueStr = typedArray.getString(R.styleable.FormKeyValueView_value);
        if (labelRes != 0) {
            label = getContext().getResources().getString(labelRes);
        }
        if (!TextUtils.isEmpty(labelStr)) {
            label = labelStr;
        }
        if (valueRes != 0) {
            value = getContext().getResources().getString(valueRes);
        }
        if (!TextUtils.isEmpty(valueStr)) {
            value = valueStr;
        }
        float labelSize = typedArray.getDimension(R.styleable.FormKeyValueView_labelSize, NormalUtils.dip2px(getContext(), 16));
        float valueSize = typedArray.getDimension(R.styleable.FormKeyValueView_valueSize, NormalUtils.dip2px(getContext(), 14));
        typedArray.recycle();
        View view = View.inflate(context, R.layout.item_abe_view_common_form_key_value_text, this);
        itemLabelName = view.findViewById(R.id.item_label_name);
        itemValue = view.findViewById(R.id.item_value);
        itemLabelName.setText(label);
        itemValue.setText(value);
        itemLabelName.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize);
        itemValue.setTextSize(TypedValue.COMPLEX_UNIT_PX, valueSize);
    }

    public TextView getItemValue() {
        return itemValue;
    }

    public void setLabel(String label) {
        this.label = label;
        if (itemLabelName != null) {
            itemLabelName.setText(label);
        }
    }

    public void setLabelRes(int labelRes) {
        setLabel(getContext().getResources().getString(labelRes));
    }

    public void setValue(String value) {
        this.value = value;
        if (itemValue != null) {
            itemValue.setText(value);
        }
    }

    public void setValueRes(int valueRes) {
        setValue(getContext().getResources().getString(valueRes));
    }

    public String getText() {
        return itemValue.getText() == null ? "" : itemValue.getText().toString().trim();
    }
}
